package com.raise.drive.model.dao.impl;

import com.raise.drive.model.dao.SectionDao;
import com.raise.drive.model.domain.Section;
import com.raise.drive.model.search.SectionSearchRequest;
import org.hibernate.criterion.DetachedCriteria;

import java.util.List;

/**
 * Author: Evgeny Morozov
 */
public class SectionDaoImpl extends BaseDaoImpl<Section> implements SectionDao<Section> {

    @Override
    protected Class<Section> getDomainClass() {
        return Section.class;
    }

    public Section get(Long id) {
        return getRequiredObject(id);
    }

    public List<Section> getAll() {
        return getObjects();
    }

    public void save(Section obj) {
        saveObject(obj);
    }

    public void delete(Section obj) {
        deleteObject(obj);
    }

    public List<Section> findSections(SectionSearchRequest searchRequest) {
        return getObjectsByRequest(searchRequest, createCriteria(searchRequest));
    }

    public int getSectionsCount(SectionSearchRequest searchRequest) {
        return getObjectsCount(createCriteria(searchRequest));
    }

    /**
     * Creates a basic criteries from search requests. Returned criteria defines
     * the order to fetch data from database and the field to sort data by. To
     * extend the abilities of this method you should override it in children
     * classes.
     *
     * @param request - request to create criteria from
     * @return criteria to search entities
     */
    private DetachedCriteria createCriteria(SectionSearchRequest request) {
        DetachedCriteria criteria = DetachedCriteria.forClass(getDomainClass());
        String sortedField = request.getSortedField();
        if (sortedField == null || sortedField.length() == 0) {
            throw new IllegalArgumentException("sorted field is not specefied");
        }

        return criteria;
    }

    
}
